const bcrypt = require('bcryptjs');
const { Users } = require('../../model/user')
module.exports = async(req, res, next) => {

    const { username, email, password, role, state } = req.body;
    const id = req.query.id;
    let user = await Users.findOne({ _id: id });
    let isValid = await bcrypt.compare(password, user.password);
    if (isValid) {
        await Users.updateOne({ _id: id }, {
            username: username,
            email: email,
            role: role,
            state: state
        })
        res.redirect('/admin/user')
    } else {
        let obj = { path: '/admin/user-edit', message: '密码比对失败', id: id }
        next(JSON.stringify(obj))
    }

}